package com.excel.generation;

import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class Main {
    public static void main(String[] args) {
        String readPath = "E:\\IDEA\\IdeaProjects\\excel-generation\\src\\main\\resources\\template\\导入数据.xlsx";

        List<WriteExcelEntity> data = new ArrayList<>();
        List<Map<String, String>> projectManagementItems = new ArrayList<>();

        List<String> projectManagementItemsPrefix = new ArrayList<>();

        EasyExcel
                .read(readPath, ReadExcelEntity.class, new ExcelListener(projectManagementItems))
                .headRowNumber(1)
                .sheet()
                .doRead();


        projectManagementItemsPrefix.add("");
        projectManagementItemsPrefix.add("新增");
        projectManagementItemsPrefix.add("修改");
        projectManagementItemsPrefix.add("删除");
        projectManagementItemsPrefix.add("批量删除");
        projectManagementItemsPrefix.add("查询");
        projectManagementItemsPrefix.add("导出");

        projectManagementItemsPrefix.add("记录/操作日志");
        projectManagementItemsPrefix.add("分配");
        projectManagementItemsPrefix.add("共享");
        projectManagementItemsPrefix.add("取消共享");
        projectManagementItemsPrefix.add("权限校验");
        projectManagementItemsPrefix.add("打印");
        projectManagementItemsPrefix.add("统计");

        for (Map<String, String> projectManagementItem : projectManagementItems) {
            for (Map.Entry<String, String> entry : projectManagementItem.entrySet()) {
                for (String prefix : projectManagementItemsPrefix) {
                    WriteExcelEntity writeExcelEntity = new WriteExcelEntity();
                    if (prefix.isEmpty()) {
                        writeExcelEntity.setFirstLine(entry.getKey());
                    } else {
                        writeExcelEntity.setFirstLine("");
                    }

                    String secondLine;
                    if (prefix.contains("权限校验")) {
                        secondLine = entry.getValue() + prefix;
                    } else if (prefix.contains("/")) {
                        String[] split = prefix.split("/");
                        secondLine = split[0] + entry.getValue() + split[1];
                    } else {
                        secondLine = prefix + entry.getValue();
                    }

                    writeExcelEntity.setSecondLine(secondLine);
                    data.add(writeExcelEntity);
                }
            }
        }

        String writePath = "E:\\IDEA\\IdeaProjects\\excel-generation\\src\\main\\resources\\template\\导出数据.xlsx";


        // 写入Excel
        EasyExcel.write(writePath)
                .sheet(0)
                .head(WriteExcelEntity.class)
                .doWrite(data);

        log.info("数据写入完成！");
    }
}